﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class PhongHocProvider
    {
        public static List<PhongHoc> LayDanhSachPhongHoc()
        {
            List<PhongHoc> lst = new List<PhongHoc>();
            using(QLTTTHEntities context = new QLTTTHEntities())
            {
                var query = (from u in context.PhongHocs select u);
                foreach(var row in query)
                {
                    PhongHoc ph = new PhongHoc();

                    ph.MaPhong = row.MaPhong;
                    ph.TenPhong = row.TenPhong;
                    ph.ChucNang = row.ChucNang;
                    ph.TrangThai = row.TrangThai;

                    lst.Add(ph);
                }
            }
            return lst;
        }
        public static bool XoaPhongHoc(int maPH)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.PhongHocs
                                 where u.MaPhong.Equals(maPH)
                                 select u).First();

                    context.PhongHocs.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CapNhatPhongHoc(PhongHoc ph)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.PhongHocs
                                 where u.MaPhong.Equals(ph.MaPhong)
                                 select u).First();
                    query.TenPhong= ph.TenPhong;
                    query.ChucNang = ph.ChucNang;
                    query.ChucNang = ph.TrangThai;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ThemPhongHoc(PhongHoc ph)
        {
            int maPH = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.PhongHocs.Add(ph);
                    context.SaveChanges();
                    maPH = ph.MaPhong;
                }
            }
            catch
            {
            }
            return maPH;
        }
    }
}
